package com.bdcom.autotest;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import com.bdcom.autotest.interaction.db.CheckMysql;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bdcom.autotest.interaction.serial.ui.SerialPortFrame;
import com.bdcom.autotest.interaction.socket.DevSocket;
import com.bdcom.autotest.interaction.step.FlowBean;
import com.bdcom.autotest.interaction.step.StepBean;
import com.bdcom.autotest.interaction.step.StepThread;
import com.bdcom.autotest.ui.BottomPanel;
import com.bdcom.autotest.ui.CenterPanel;
import com.bdcom.autotest.ui.LeftPanel;
import com.bdcom.autotest.ui.TopPanel;
import com.bdcom.autotest.util.CUtil;
import com.bdcom.autotest.util.Prop;
import com.bdcom.autotest.util.UIUtil;


public class MainFrame extends JFrame{

	
	private static Logger log = LoggerFactory.getLogger(MainFrame.class);
	
	private static MainFrame mainFrame = null;
	
	private int ww = 1300;
	private int hh = 700;
	
	private StepThread curretThread;
	
	public static MainFrame getInstance() {
		if( mainFrame == null  ) {
			mainFrame = new MainFrame();
		}
		return mainFrame;
	}
	
	
	public MainFrame() {
		
		try {
			UIUtil.initSkin();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		initComponent();
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				int result = JOptionPane.showConfirmDialog(mainFrame , ("确定要关闭 ?") );

				if( 0 == result ){
					CheckMysql checkMySQL = new CheckMysql();
					try {
						checkMySQL.stopMysql();
					} catch (Exception exception) {
						exception.printStackTrace();
					}
					System.exit(0);
				}

			}
		});
	}
	
	private TopPanel topp = null;
	private LeftPanel leftp = null;
	private CenterPanel centerp = null;
	private BottomPanel bottomp = null;
	public SerialPortFrame serialPort = null;


	private void initComponent() {
		this.setTitle("BDCOM AUTO TEST");
		Container contentPane = this.getContentPane();
		contentPane.setBackground(Color.WHITE);
		topp = new TopPanel(this);
		leftp = new LeftPanel(this);
		centerp = new CenterPanel(this);
		bottomp = new BottomPanel(this);
//		serialPort = new SerialPortFrame();
		contentPane.add(topp, BorderLayout.NORTH);
		contentPane.add(leftp, BorderLayout.WEST );
		contentPane.add(centerp, BorderLayout.CENTER );
		contentPane.add(bottomp, BorderLayout.SOUTH );
		
		topp.changeFlow();
	}

	
	BootThread bootThread;
	public void showFrame(BootThread bootThread) {
		this.bootThread = bootThread;
		this.setSize(ww, hh);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}

	public TopPanel getTopp() {
		return topp;
	}

	public LeftPanel getLeftp() {
		return leftp;
	}

	public CenterPanel getCenterp() {
		return centerp;
	}

	public BottomPanel getBottomp() {
		return bottomp;
	}

	public StepThread getCurretThread() {
		return curretThread;
	}
	
	public SerialPortFrame getSerialPort() {
		return serialPort;
	}

	public void setSerialPort(SerialPortFrame serialPort) {
		this.serialPort = serialPort;
	}
	public void setCurretThread(StepThread curretThread ) {
		if(  curretThread ==null  ) {
			
		} else {
//			leftp.startTestBtn(  );
		}
		this.curretThread = curretThread;
	}
	
	public void endTest(boolean flag) {
		if(  flag ) {
			leftp.emptysn();
		}
	}

	
	public void startANewTest() {
		StepThread thread = this.getCurretThread();
		if(thread!=null){
			thread.goDoRun();
			thread.setEndFlag(true);
			thread.setRestartFlag(true);
//			DevSocket devSokcet = thread.getDevSokcet();
//			int count = 0;
//			while(devSokcet==null&&count<5){
//				devSokcet = thread.getDevSokcet();
//				try {
//					count++;
//					Thread.sleep(1000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//			if(devSokcet!=null){
//				devSokcet.shutdown();
//			}
		}
		this.setCurretThread(null);
		endTest(true);
		topp.clickCheckBox();
	}

	public static void main(String[] args) {
		
		Prop.init(args);
		MainFrame.getInstance().showFrame(null);
		
	}
	
	
	
	public void tables() {
		// 表格中显示的数据、
		Object rows[][] = { { "王鹏", "江西", "43" }, { "周丹", "四川", "25" }, { "钱丽", "贵州", "32" }, { "孙军", "新疆", "24" },
				{ "李欢", "江苏", "45" }, { "苏菲", "广东", "33" } };
		String columns[] = { "姓名", "籍贯", "年龄" };
		TableModel model = new DefaultTableModel(rows, columns);
		JTable table = new JTable(model);
		RowSorter sorter = new TableRowSorter(model);
		table.setRowSorter(sorter);
		JScrollPane pane = new JScrollPane(table);
		this.add(pane, BorderLayout.CENTER);
	}


	public void setflow(FlowBean flowBean) {
		
		ArrayList<StepBean> steps = flowBean.getSteps();
		centerp.setSteps(steps);
		
		leftp.setflow(flowBean);
		
	}
	
	public void appendLog(String log, boolean isError) {
		
		leftp.addLog( CUtil.getTimeStr() +  ": " + log + "\n\n" );
		
	}

	public void appendLog2(String log, boolean isError) {
		
		leftp.addLog( CUtil.getTimeStr() +  ": " + log + "\n" );
		
	}

	public void setCheckBox(boolean b) {
		topp.setcheckbox(b);
	}


	public void scrollFlow(StepBean bean) {
		int a = 0;
		ArrayList<StepBean> steps2 = bean.getParent().getSteps();
		int size = steps2.size();
		for(  int i=0; i<size ;i++  ) {
			StepBean ttt = steps2.get(i);
			a = a + ttt.getHh() + 14;
			if(  bean == ttt  ) {
				break;
			}
		}
		centerp.scrollFlow( a  );
	}
	
	
	
}
